<template>
	<view class="login-content">
		<view class="title">
			<span>登录</span>
		</view>
		<view class="login-input-row">
			<view class="user" v-if="loginType">
				<u-input placeholder="输入手机号" v-model="mobile" clearable type="number" style="height:90rpx">
				</u-input>
			</view>
			<view class="user" v-else>
				<u-input placeholder="输入账号" v-model="username" clearable type="text" style="height:90rpx">
				</u-input>
			</view>
			<view class="code" v-if="loginType">
				<u-input placeholder="请输入验证码" v-model="code" clearable style="height:90rpx;width: 70%;">
				</u-input>
				<!-- <u-toast ref="uToast"></u-toast> -->
				<u-verification-code :seconds="seconds" end-text="重新获取验证码" ref="uCode" @change="codeChange">
				</u-verification-code>
				<span class="get-code" @tap="getCode">{{tips}}</span>
			</view>
			<view class="code" v-else>
				<u-input type="password" placeholder="请输入密码" v-model="password" clearable style="height:90rpx">
				</u-input>
			</view>
			<view class="forget-password-row" v-if="!loginType">
				<span class="forget-password" @tap="forgetHandle">忘记密码</span>
			</view>
			<view class="login-row" :style="loginType?'margin-top:60rpx':'margin-top:50rpx'">
				<u-button type="default" shape="circle" text="登录" class="login-btn" @click="login" hover-class="none">登录
				</u-button>
			</view>
			<view class="select-type">
				<span @tap="changeType">{{changeTextShow()}}</span>
			</view>
		</view>
		<view class="other-way-row">
			<view class="select-way">
				<!-- <span @tap="wakeUpHandle('wx')">
					<image src="@/static/img/login/wx.png" alt=""></image>
				</span>
				<span @tap="wakeUpHandle('qq')">
					<image src="@/static/img/login/qq.png" alt=""></image>
				</span>
				<span @tap="wakeUpHandle('wb')">
					<image src="@/static/img/login/wb.png" alt=""></image>
				</span> -->
			</view>
			<span class="prompt">验证即登录，未注册则自动创建新账号</span>
		</view>
		<!-- <myVerification v-if="codeFlag" @send="verification" class="verification" ref="verification"
			business="LOGIN" /> -->
		<!-- <u-loading :show="isLoading"></u-loading> -->
	</view>
</template>

<script>
	import { API } from '../../config/myApi.js';
	import storage from "@/utils/storage.js";
	import myVerification from "@/components/verification/verification.vue"; //验证码模块
	import uuid from "@/utils/uuid.modified.js"; // uuid
	export default {
		components: {
			myVerification,
		},
		data() {
			return {
				flage: false, //是否验证码验证
				codeFlag: true, //验证开关，用于是否展示验证码
				loginType: true,
				mobile: '',
				code: '',
				userType: '',
				password: '',
				username: "",
				isFirst: true,
				//角色
				role: '',

				tips: '',
				// refCode: null,
				seconds: 60,
				// isLoading:false,
			}
		},
		onLoad() {
			storage.setUuid(uuid.v1());
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					this.getCodeHandle()
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();

					}, 0);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			//获取验证码
			getCodeHandle() {
				const pram = {
					mobile: this.mobile,
					verificationEnums: 'LOGIN'
				}
				API.getCodeByPhone(pram.verificationEnums, pram.mobile).then(res => {
					// console.log('获取验证码',res)
				})

			},
			login() {
				// console.log(this.loginType,'类型')
				if (this.loginType) {
					this.smsLogin();
				} else {
					this.userLogin()
				}
			},
			 userLogin() {
				const pwd = this.md5(this.password);
				let pram = {
					username: this.username,
					password: pwd,
					// uuid: this.$store.state.uuid,
				};
				// console.log("参数", pram);
				 API.userLogin(pram).then( (res) => {
					this.commonLoginHandle(res)
				})
			},
			async commonLoginHandle(res){
				console.log('登录res', res);
				if (res.code === 200) {
					const {
						accessToken,
						exists,
						refreshToken
					} = res.result
					storage.setAccessToken(accessToken);
					storage.setRefreshToken(refreshToken);
					storage.setExists(exists);
					await this.$store.dispatch('A_userInfo'); // 个人信息
					await this.$store.dispatch('A_workbenchData'); // 工作台
					uni.showToast({
						title:'登录成功',
						success: () => {
							uni.reLaunch({
								url: '/pages/tabbar/home/index'
							})	
						}
					})
					
				} else {
					// console.log('不行');
					uni.showToast({
						title:res.message,
						duration: this.$store.state.toastDuration,
						icon: "none",
					})
				}
			},
			// 短信登录
			smsLogin() {
				const pram = {
					mobile: this.mobile,
					code: this.code
				}
				console.log('短信不行');
				API.smsLogin(pram).then(res => {
					// this.isLoading = false;
					console.log('短信登录', res)
					this.commonLoginHandle(res)
				})
			},
			//切换登录方式
			changeType() {
				// 置空
				this.mobile = '';
				if (this.loginType) {
					this.code = '';
				} else {
					this.password = '';
				}
				//改变状态🐴
				this.loginType = !this.loginType;
			},
			//切换显示文本
			changeTextShow() {
				if (this.loginType) {
					return "帐号密码登录"
				} else {
					return "验证码登录"
				}
			},
			//唤醒其他登录
			wakeUpHandle(methods) {
				console.log(methods);
			},
			//忘记密码
			forgetHandle() {
				uni.navigateTo({
					url: "/pages/passport/forgetPassword"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login-content {
		min-height: calc(100vh - 88rpx);
		height: 100%;
		background-color: #fff;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.title {
			font-size: 46rpx;
			font-weight: bold;
			color: #333333;
			margin-top: 207rpx;
			text-align: center;
		}

		.login-input-row {
			padding: 0rpx 50rpx;
			margin-top: 180rpx;

			.user,
			.code {
				border-bottom: 1rpx solid #E3E3E3;
			}

			.code {
				margin-top: 50rpx;
				position: relative;

				.get-code {
					font-size: 26rpx;
					color: #F4333C;
					position: absolute;
					right: 0;
					top: 18rpx;
				}
			}

			.forget-password-row {
				margin-top: 26rpx;
				text-align: right;

				.forget-password {
					font-size: 26rpx;
					color: #F4333C;
				}
			}

			.login-row {
				.login-btn {
					background-color: #F4333C;
					color: #fff;
					height: 90rpx;
				}

				.u-hairline-border:after {
					border: none;
				}
			}

			.select-type {
				font-size: 26rpx;
				color: #F4333C;
				margin-top: 40rpx;
				text-align: center;
			}
		}

		.other-way-row {
			flex: 1;
			width: 100%;
			// height: auto;
			position: relative;
			margin-bottom: 80rpx;
			display: flex;
			flex-direction: column;
			justify-content: flex-end;

			.select-way {
				display: flex;
				justify-content: center;
				// width:400rpx;
				height: 70rpx;

				>span {
					width: 150rpx;
					text-align: center;

					image {
						width: 70rpx;
						height: 70rpx;
					}
				}

				>text:nth-of-type(2) {
					margin: 0rpx 60rpx;
				}
			}

			.prompt {
				font-size: 22rpx;
				color: #666666;
				display: block;
				text-align: center;
				margin-top: 40rpx;
			}
		}
	}
</style>
